package com.jing.android.notes;

import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.SystemClock;
import android.util.Log;

import androidx.appcompat.app.AppCompatDelegate;
import androidx.core.app.JobIntentService;

import com.jing.android.notes.constant.Constants;
import com.jing.android.notes.constant.Keys;
import com.jing.android.notes.ui.service.InitService;
import com.jing.android.notes.ui.util.ActivityLifecycle;
import com.tencent.tinker.lib.tinker.TinkerInstaller;
import com.tencent.tinker.loader.app.DefaultApplicationLike;
import com.umeng.analytics.MobclickAgent;
import com.umeng.commonsdk.UMConfigure;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;


public class NotesAppLike extends DefaultApplicationLike {

    private static final String TAG = NotesAppLike.class.getSimpleName();

    public NotesAppLike(Application application, int tinkerFlags, boolean tinkerLoadVerifyFlag, long applicationStartElapsedTime, long applicationStartMillisTime, Intent tinkerResultIntent) {
        super(application, tinkerFlags, tinkerLoadVerifyFlag, applicationStartElapsedTime, applicationStartMillisTime, tinkerResultIntent);
    }

    @Override
    public void onBaseContextAttached(Context base) {
        super.onBaseContextAttached(base);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS", Locale.getDefault());
        Log.i(TAG, "1, " + dateFormat.format(new Date(SystemClock.elapsedRealtime())));
        Log.i(TAG, "2, " + dateFormat.format(new Date(System.currentTimeMillis())));
        Log.i(TAG, "3, " + Build.FINGERPRINT);
        TinkerInstaller.install(this);
    }

    @Override
    public void onCreate() {
        super.onCreate();
        Application application = getApplication();
        application.registerActivityLifecycleCallbacks(ActivityLifecycle.getInstance());
        JobIntentService.enqueueWork(getApplication(), InitService.class, InitService.JOB_ID, new Intent());
        UMConfigure.preInit(getApplication().getApplicationContext(), null, null);
        //获取启动配置
        SharedPreferences preferences = application.getSharedPreferences( Constants.PREFERENCE_NAME_START, Context.MODE_PRIVATE);
        boolean agreeToPrivacyPolicy = preferences.getBoolean(Keys.AGREE_TO_PRIVACY_POLICY, false);
        if (agreeToPrivacyPolicy) {
            Log.i(TAG, "UM Start");
            UMConfigure.init(application.getApplicationContext(), UMConfigure.DEVICE_TYPE_PHONE, "");
            UMConfigure.setLogEnabled(true);
            MobclickAgent.setPageCollectionMode(MobclickAgent.PageMode.AUTO);
            Log.i(TAG, "UM End");
        }
        //深色模式默认跟随系统
        int modeNight = preferences.getInt(Keys.MODE_NIGHT, AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
        AppCompatDelegate.setDefaultNightMode(modeNight);
        Log.i(TAG, "onCreate");
    }
}
